package com.know.dao;

import java.util.List;

import my.db.QueryHelper;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Users;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;


public class UserDao {
	/**
	 * 验证通过增加新用户
	 * */
	public void save(Users user) {
		String sql = "insert into users (`user_id`, `user_name`, `user_password`, `user_email`,`user_level`,`user_reg_time`) values(null,?,?,?,?,?)";
		QueryHelper.update(sql,user.getUser_name(),user.getUser_password(),user.getUser_email(),"1",user.getUser_reg_time());
	}
	/**
	 * 分页查询
	 */
	public Record findPage(Page<Users> page) {
		Record record = new Record();
		String sql = "select * from users";
		if(StringUtils.isNotBlank(page.getOrderBy())) {
			sql += String.format(" order by %s %s", page.getOrderBy(), page.getOrder());
		}
		
		List<Users> list = QueryHelper.queryPage(Users.class, sql, page.getPageNo(), page.getPageSize());

	
		//统计总数count
		String sql2 = sql.toLowerCase().replaceFirst("select .*? from", "select count(*) from");
		long count = QueryHelper.stat(sql2);
		
		record.setCount(count);
		record.setData(list);
		
		return record;
	
	}
	/**
	 * 根据ID查找
	 */
	public Users finById(long parseLong) {
		String sql="select *from users where user_id=?";
		Users user=QueryHelper.queryUnique(Users.class, sql, parseLong);
		return user;
	}
	/**
	 * 登陆账号验证账号密码
	 * @param user_name
	 * @param user_password
	 * @return
	 */
	public boolean isUser(String user_name, String user_password){
		String sql="select count(*) from users where user_name=? and user_password=?";
		return QueryHelper.stat(sql, user_name,user_password)==1? true:false;
	}
	/**
	 * 进行账号验证，账号合法性.
	 * 数据库验证账号
	 * @param user_name
	 * @return
	 */
	public boolean isUser(String user_name) {
		String sql="select count(*)from users where user_name=?";
				
		return QueryHelper.stat(sql, user_name)==0? true:false;
	}
	/**
	 * 查询用户信息ID
	 */
	public Users query(Long id){
		String sql = "select * from users where user_id=?";
		return QueryHelper.queryUnique(Users.class, sql, id);
	}
	/**
	 * 查询用户信息 name
	 */
	public Users query(String name){
		String sql = "select * from users where user_name=?";
		return QueryHelper.queryUnique(Users.class, sql, name);
	}
	/**
	 * 删除用户
	 */
	public void del(Long id){
		String sql = "delete from users where user_id=?";
		QueryHelper.update(sql, id);
	}
	/**
	 * 批量删除用户
	 */
	public void Muldel(String ids){
		String sql = "delete from users where user_id in (" + ids + ")";
		QueryHelper.update(sql);
	}

	/**
	 * 最热用户前Num个（根据评论）
	 */
	public  List<Users> hotusers(int num){
		String sql = "select user_name,user_level,count(comments.comment_id) AS user_temp from users,comments where users.user_id=comments.user_id GROUP BY comments.user_id  ORDER BY user_temp DESC LIMIT ?";
		String sql_load="select *from users where user_id=?";
		List<Users> list=QueryHelper.query(Users.class, sql,num);
		//无头像加载默认头像
		for (Users user : list) {
			String src="img/user.jpg";
			if(user.getUser_photo()==null||user.getUser_photo().equals(""))
				user.setUser_photo(src);
		}
		return list; 
	}
	public Record update(Users user) {
		String sql="UPDATE know.users set user_name=?,  user_email=?, user_photo=?, user_birthday=?, user_age=?, user_tel=?, user_nickname=? WHERE (user_id=?)";
		String sql2="UPDATE know.users set user_password=? where user_id=? ";
		//用户名为空 执行更新密码
		if(user.getUser_name()==null||user.getUser_name().equals(""))
			QueryHelper.update(sql2, user.getUser_password(),user.getUser_id());
		else
			QueryHelper.update(sql, user.getUser_name(),user.getUser_email(),user.getUser_photo(),user.getUser_birthday(),user.getUser_age(),user.getUser_tel(),user.getUser_nickname(),user.getUser_id());
		
		return new Record(true);
	}
	public Record updateall(Users user) {
		String sql="UPDATE know.users set user_name=?,  user_email=?, user_photo=?, user_birthday=?, user_age=?, user_tel=?, user_nickname=? WHERE (user_id=?)";
		String sql2="UPDATE know.users set user_password=? where user_id=? ";

			QueryHelper.update(sql2, user.getUser_password(),user.getUser_id());
			QueryHelper.update(sql, user.getUser_name(),user.getUser_email(),user.getUser_photo(),user.getUser_birthday(),user.getUser_age(),user.getUser_tel(),user.getUser_nickname(),user.getUser_id());
		
		return new Record(true);
	}

	
}
